package com.cmcc.cmii.smsmail.listener;

import com.cmcc.cmii.common.utils.JsonUtil;
import com.cmcc.cmii.smsmail.constants.MailMqConstants;
import com.cmcc.cmii.smsmail.entity.MailSendStatus;
import com.cmcc.cmii.smsmail.model.BaseMail;
import com.cmcc.cmii.smsmail.model.enums.MailSendStatusEnum;
import com.cmcc.cmii.smsmail.service.IMailSendStausService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author Thanks
 * 监听 发送状态的类  进行发送状态的写入及更新操作
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class MailSendStatusListener {

    private final IMailSendStausService iMailSendStausService;

    /**
     * 处理发送状态
     * @param jsonStr
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MailMqConstants.MAIL_SEND_STATUS_QUEUE, durable = "true"),
            exchange = @Exchange(value = MailMqConstants.MAIL_SEND_STATUS_EXCHANGE,
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC, durable = "true"), key = MailMqConstants.MAIL_SEND_STATUS_KEY))
    public void handle(String jsonStr){
        try{
            BaseMail baseMail = (BaseMail)JsonUtil.getModelFromJson(jsonStr, BaseMail.class);
            Integer sendStatus = baseMail.getSendStatus();
            MailSendStatus mailSendStatus = new MailSendStatus();
            //发送中直接保存
            if(MailSendStatusEnum.SENDING.getCode().equals(sendStatus)){
                mailSendStatus.setMailFrom(baseMail.getMailFrom());
                mailSendStatus.setMailTo(JsonUtil.toJSONString(baseMail.getListMailTo()));
                mailSendStatus.setSysId(baseMail.getSysId());
                mailSendStatus.setReqId(baseMail.getReqId());
                mailSendStatus.setSysName(baseMail.getSysName());
                mailSendStatus.setSendStatus(sendStatus);
                mailSendStatus.setCreateTime(new Date());
                mailSendStatus.setUpdateTime(mailSendStatus.getCreateTime());
                iMailSendStausService.save(mailSendStatus);
            }else{
                String reqId = baseMail.getReqId();
                mailSendStatus.setReqId(reqId);
                MailSendStatus dbMailSendStatus = iMailSendStausService.queryOne(mailSendStatus);
                if(null == dbMailSendStatus){
                    log.error("====================MailSendStatusListener根据reqId未找到发送信息=============={}",jsonStr);
                    return;
                }
                dbMailSendStatus.setSendStatus(sendStatus);
                dbMailSendStatus.setUpdateTime(new Date());
                iMailSendStausService.update(dbMailSendStatus);
            }

        }catch (Exception e){
            e.printStackTrace();
            log.error("==================MailSendStatusListener=发现非邮件类型================{}",jsonStr);
        }


    }

}
